<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="parentUserDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="rules">
				<el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="学生ID" >
              <el-input v-model="state.studentRow.id" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="学生姓名" >
              <el-input v-model="state.studentRow.nickName" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="家长手机号" prop="username">
							<el-input v-model="state.ruleForm.username" placeholder="请输入家长手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="家长姓名" prop="nickName">
							<el-input v-model="state.ruleForm.nickName" placeholder="请输入家长姓名" clearable></el-input>
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="关系" prop="gender">
              <el-select v-model="state.ruleForm.gender" placeholder="请选择" clearable class="w100">
                <el-option label="父子关系"  :value="0"></el-option>
                <el-option label="母子关系"  :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">-->
<!--						<el-form-item label="关联角色">-->
<!--							<el-select v-model="state.ruleForm.roleSign" placeholder="请选择" clearable class="w100">-->
<!--								<el-option label="超级管理员" value="admin"></el-option>-->
<!--								<el-option label="普通用户" value="common"></el-option>-->
<!--							</el-select>-->
<!--						</el-form-item>-->
<!--					</el-col>-->


<!--					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">-->
<!--						<el-form-item label="账户密码">-->
<!--							<el-input v-model="state.ruleForm.password" placeholder="请输入" type="password" clearable></el-input>-->
<!--						</el-form-item>-->
<!--					</el-col>-->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
              <el-select v-model="state.ruleForm.isDelete" placeholder="请选择"  class="w100" :disabled="state.ruleForm.isDelete==0">
                <el-option label="正常"  :value="0"></el-option>
                <el-option label="禁用"  :value="1"></el-option>
              </el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(parentUserDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="parentUserDialog">
import { reactive, ref} from 'vue';
import {ElMessage, FormInstance, FormRules} from 'element-plus'
import {userApi} from "/@/api/user";

const {saveOrUpdateParent} = userApi();
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const validatePhone = (rule: any, value: string, callback: any) => {
  const phoneRegex = /^1[3-9]\d{9}$/;
  if (!value) {
    callback(new Error('请输入家长电话号码'));
  } else if (!phoneRegex.test(value)) {
    callback(new Error('手机号格式不正确'));
  } else {
    callback();
  }
};
const rules = reactive<FormRules<RowStudentUserType>>({
  username: [
    {required: true, validator: validatePhone, trigger: 'blur' },
    //{ min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  nickName: [
    { required: true, message: '请输入家长姓名', trigger: 'blur' },
    //{ min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  gender: [
    {
      required: true,
      message: '请选择关系',
      trigger: 'change',
    },
  ],

})

// 定义变量内容
const parentUserDialogFormRef = ref();
const state = reactive({
	ruleForm: {
    id: '',
    nickName: '',
    username:'',
    isDelete:0,
    gender: 0,
	},
  studentRow:{
    id: '',
    nickName: '',
  },
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, studentRow:RowStudentUserType,row: RowStudentUserType) => {
  // console.log("student");
  // console.log(studentRow)
  // console.log("parent");
  // console.log(row);
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
    state.studentRow = JSON.parse(JSON.stringify(studentRow));
		state.dialog.title = '修改家长信息';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增家长';
		state.dialog.submitTxt = '新 增';
    state.studentRow = JSON.parse(JSON.stringify(studentRow));
    clearForm();
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
// const onSubmit = () => {
// 	closeDialog();
// 	emit('refresh');
// 	// if (state.dialog.type === 'add') { }
// };
const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, ) => {
    if (valid) {
      const res = await saveOrUpdateParent(
          {
            parentId:state.ruleForm.id,
            nickName:state.ruleForm.nickName,
            username:state.ruleForm.username,
            studentId:state.studentRow.id,
            userMobile:state.ruleForm.username,
            gender:state.ruleForm.gender,
            isDelete:state.ruleForm.isDelete,
          }
      )
      if (res.status === 200) {
        ElMessage.success('更改成功');
        emit('refresh');
      }else{
        ElMessage.error('更改失败');
      }
      closeDialog();

    }
  })
}



const clearForm = () => {
  state.ruleForm = {
    id: '',
    nickName: '',
    username:'',
    isDelete:0,
    gender: 0,
  }

}
// 暴露变量
defineExpose({
	openDialog,
});
</script>
